/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFileChooser;

/**
 *
 * @author banhbaochay
 */
public class ImagePreview extends JComponent implements PropertyChangeListener {

    ImageIcon thumbnail = null;
    File file = null;
    JFileChooser fc;

    public ImagePreview(JFileChooser fc) {
        this.fc = fc;
//            Default size cua phan hien thu muc la: 400x164
//            neu chieu cao < 164 thi se duoc lam tron thanh 164
        setPreferredSize(new Dimension(600, 400));
        fc.addPropertyChangeListener(this);
    }

    public void propertyChange(PropertyChangeEvent evt) {
        boolean update = false;
        String prop = evt.getPropertyName();
        if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) {
            file = null;
            update = true;
        } else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
//            file = (File) evt.getNewValue();
            File[] files = fc.getSelectedFiles();
            if (files.length > 0) {
                file = files[files.length - 1];
            }
            update = true;
        }


        if (update) {
            thumbnail = null;
            if (isShowing()) {
                loadImage();
                repaint(); //call paintComponent() method
            }
        }
    }

    private void loadImage() {
        if (file == null) {
            thumbnail = null;
            return;
        }

        ImageIcon tmpIcon = new ImageIcon(file.getPath());
        if (tmpIcon != null) {
//                Neu chieu rong > 590, scale thanh chieu rong = 590, chieu dai
//                chieu cao de -1 --> co ve sao cho ti le rong/cao bang dung
//                anh goc

            int width = tmpIcon.getIconWidth();
            int height = tmpIcon.getIconHeight();

            if (width > height && width > 590) {
                thumbnail = new ImageIcon(tmpIcon.getImage().
                        getScaledInstance(590, -1, Image.SCALE_FAST));
            } else if (width < height && height > 400) {
                thumbnail = new ImageIcon(tmpIcon.getImage().
                        getScaledInstance(-1, 400, Image.SCALE_FAST));
            } else {
                thumbnail = tmpIcon;
            }
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
//            kiem tra thumbnail truoc khi ve vi paintComponent se duoc tu dong
//            goi khi thay doi size cua component hay phong to cua so...
        if (thumbnail != null) {
//                Ve thumbnail o giua cua Component (size o setPreffedSize)
            int x = getWidth() / 2 - thumbnail.getIconWidth() / 2;
            int y = getHeight() / 2 - thumbnail.getIconHeight() / 2;

            if (y < 0) {
                y = 0;
            }

            if (x < 5) {
                x = 5;
            }
            thumbnail.paintIcon(this, g, x, y);
        }
    }
}
